package com.googlecode.lightest.core

class ConfigurationTest extends GroovyTestCase {
    def config
    
    @Override
    void setUp() {
        super.setUp()
        config = new Configuration()
    }
    
    void testSetClassPaths() {
        def configText =
'''
config {
    classPaths {
        path ('foo')
        path ('bar')
    }
}
'''

        config.init(configText)
        
        def classPaths = config.getClassPaths()
        
        assertEquals(3, classPaths.size())
        assertEquals('.', classPaths[0])
        assertEquals('foo', classPaths[1])
        assertEquals('bar', classPaths[2])
    }
    
    void testDefaultOutputDir() {
        def outputDir = new File('lightest-report').getCanonicalPath()
        
        config.init("")
        assertEquals(outputDir, config.getOutputDir())
    }
    
    void testSetOutputDir() {
        def outputDir = new File('foo/bar').getCanonicalPath()
        def configText =
"""
config {
    outputDir ('${LightestUtils.backslash(outputDir)}')
}
"""
    
        config.init(configText)
        
        assertEquals(outputDir, config.getOutputDir())
    }

    void testSetPreferences() {
        def configText =
'''
config {
    prefs (class: 'com.googlecode.lightest.core.DummyPreferences') {
        foo ('deluxe')
        bar (0)
    }
}
'''
        config.init(configText)
        
        def prefs = config.getPreferences()
        
        assertEquals('com.googlecode.lightest.core.DummyPreferences',
            prefs.class.name)
        assertEquals('deluxe', prefs.getFoo())
        assertEquals(0, prefs.getBar())
    }
    
    void testDefaultEnvironments() {
        config.init("")
        
        def envs = config.getEnvironments()
        
        assertEquals(3, envs.size())
        assertEquals('unspecified1', envs[0].getId())
        assertEquals('unspecified2', envs[1].getId())
        assertEquals('unspecified3', envs[2].getId())
    }
    
    void testSetEnvironment() {
        def envClass =
            'com.googlecode.lightest.core.tutorial.TutorialEnvironment'
        def configText =
"""
config {
    envs (class: '${envClass}') {
        env (id: 'e') {
            world ('ooh')
        }
        env (id: 'mc^2') {
            world ('aah')
        }
    }
}
"""
        config.init(configText)
        
        def envs = config.getEnvironments()
        
        assertEquals(2, envs.size())
        assertEquals(envClass, envs[0].class.name)
        assertEquals('e', envs[0].getId())
        assertEquals('ooh', envs[0].getWorld())
        assertEquals(envClass, envs[1].class.name)
        assertEquals('mc^2', envs[1].getId())
        assertEquals('aah', envs[1].getWorld())
    }
    
    void testDefaultReporter() {
        config.init("")
        
        assertEquals('com.googlecode.lightest.core.LightestReporter',
            config.getReporter().class.name)
    }
    
    void testSetReporter() {
        def configText =
'''
config {
    reporter (class: 'com.googlecode.lightest.core.DummyReporter')
}
'''
        
        config.init(configText)
        
        assertEquals('com.googlecode.lightest.core.DummyReporter',
            config.getReporter().class.name)
    }
    
    void testDefaultDispatcherAssignmentStrategy() {
        config.init("")
        
        def strategy = config.getDispatcherAssignmentStrategy()
        
        assertEquals('com.googlecode.lightest.core.SimpleDispatcherAssignmentStrategy',
            strategy.class.name)
        assertTrue(strategy instanceof QueuedDispatcherAssignmentStrategy)
    }
    
    void testSetDispatcherAssignmentStrategy() {
        def strategyClass = 'com.googlecode.lightest.core.DummyStrategy'
        def configText =
"""
config {
    dispatcherAssignmentStrategy (class: '${strategyClass}')
}
"""
        
        config.init(configText)
        
        def strategy = config.getDispatcherAssignmentStrategy()
        
        assertEquals(strategyClass, strategy.class.name)
    }
}